<template>
  <div class="eidt-adress">
    <van-address-edit
      :area-list="areaList"
      show-postal
      show-delete
      show-set-default
      show-search-result
      :address-info='AddressInfo'
      @save="onSave"
      @delete="onDelete"
    />
  </div>
</template>
<script>
import { Toast } from 'vant'
import { areaList } from '@vant/area-data'
import { deleteAdress, getAdressById, updateAdress, addAdress, getidaddress } from '../../api/adress'

export default {
  data() {
    return {
      areaList,
      AddressInfo: {},
      flag: false,
      id: this.$route.query.id
    }
  },
  created() {
    this.getAdressById()
  },
  methods: {
    async onSave(content) {
      const { userid } = JSON.parse(localStorage.userlist || '[]')
      const adresslist = await getAdressById(userid)
      if (this.flag) {
        if (content.isDefault) {
          const isDefaultlist = adresslist.data.filter(item => {
            return item.isDefault === true
          })
          if (isDefaultlist.length >= 1) {
            isDefaultlist[0].isDefault = false
            await updateAdress(isDefaultlist[0].id, isDefaultlist[0])
          }
          await updateAdress(content.id, content)
        } else {
          await updateAdress(content.id, content)
        }
        const adressarr = await getAdressById(userid)
        localStorage.adress = JSON.stringify(adressarr.data)
        Toast('修改成功')
        setTimeout(() => {
          this.$router.push('/address')
        }, 1500)
      } else {
        // 新增
        const isrepeat = adresslist.data.some(item => {
          return item.name === content.name && item.areaCode === content.areaCode
        })
        if (isrepeat) {
          Toast('请勿重复添加相同地址')
          return
        }
        if (content.isDefault) {
          const isDefaultlist = adresslist.data.filter(item => {
            return item.isDefault === true
          })
          if (isDefaultlist.length >= 1) {
            isDefaultlist[0].isDefault = false
            await updateAdress(isDefaultlist[0].id, isDefaultlist[0])
          }
        }
        content.userid = userid
        content.address = content.province + '-' + content.city + '-' + content.county
        await addAdress(content)
        const adressarr = await getAdressById(userid)
        localStorage.adress = JSON.stringify(adressarr.data)
        Toast('添加成功')
        setTimeout(() => {
          this.$router.push('/address')
        }, 1000)
      }
    },
    async onDelete(content) {
      const { userid } = JSON.parse(localStorage.userlist)
      await deleteAdress(content.id)
      const adresslist = await getAdressById(userid)
      localStorage.adress = JSON.stringify(adresslist.data)
      this.$router.push('/address')
    },
    async getAdressById() {
      if (this.$route.query.id >= 1) {
        this.flag = true
        const list = await getidaddress(this.id)
        this.AddressInfo = list.data
      }
    }
  }
}
</script>

<style lang="scss">
</style>
